home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / instools / prelude.zip / LINGUA13.EXE / LINGUA.C < prev    next >
C/C++ Source or Header  |  1994-02-17  |  9KB  |  236 lines

  1. /* --------------------------------------------*\
  2. | lingua.c (version 1.3) -- (C) SichemSoft 1994 |
  3. | Roghorst 160, 6708 KS Wageningen, Netherlands |
  4. | utility for language independent applications |
  5. | author: Anneke Sicherer-Roetman, date: 930916 |
  6. \* --------------------------------------------*/
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "lingua.h"
  11.  
  12. /* opens file with error checking */
  13. static FILE *openfile(char *name,char *mode)
  14. {
  15.    FILE *fp;
  16.  
  17.    fp=fopen(name,mode);
  18.    if (!fp) printf("%s not opened\n",name);
  19.    return fp;
  20. } /* openfile */
  21.  
  22. /* writes array size in text area of file */
  23. unsigned writesize(FILE *fp,unsigned long pos,unsigned size)
  24. {
  25.    unsigned long filpos; unsigned checksum; register int i;
  26.  
  27.    filpos=ftell(fp); fseek(fp,pos,SEEK_SET);
  28.    fwrite(&size,sizeof(size),1,fp);
  29.    fseek(fp,filpos,SEEK_SET);
  30.    for (i=0,checksum=0; i<sizeof(size); i++)
  31.       checksum+=((char*)(&size))[i]^UIT_ENCRYPT;
  32.    return checksum;
  33. }
  34.  
  35. /* main text processing routine */
  36. int main(int argc,char *argv[])
  37. {
  38.    FILE *txt,*etf,*header;
  39.    char fname[81],buf[256],linefeed[3],*p,*q;
  40.    unsigned long memoffset=0,filoffset=0,offset=0;
  41.    unsigned long filpos1=0,filpos2=0,filpos3=0;
  42.    unsigned memcount=0,filcount=0,lines=0,len=0,l=0;
  43.    unsigned checksum=0,headlen=0,arrsize=0;
  44.    unsigned array=FALSE,multi=FALSE,file=FALSE;
  45.  
  46.    /* copyright message and arguments check */
  47.    puts("Lingua v1.3 - (C)1994 SichemSoft Wageningen Netherlands");
  48.    if (argc!=2 && argc!=3) {
  49.       puts("lingua <file> [<version>]"); return 1;
  50.    }
  51.  
  52.    /* open all files */
  53.    strcpy(fname,argv[1]);
  54.    if (!strchr(fname,'.')) strcat(fname,".txt");
  55.    if ((txt=openfile(fname,"r"))==NULL) return 2;
  56.    if ((header=openfile("ui_text.h","w"))==NULL) return 2;
  57.    strcpy(strchr(fname,'.')+1,"etf");
  58.    if ((etf=openfile(fname,writeRA))==NULL) return 2;
  59.  
  60.    /* start of UI_TEXT.H */
  61.    fputs("/* ui_text.h */\n\n",header);
  62.    fputs("#ifndef UI_TEXT_H\n",header);
  63.    fputs("#define UI_TEXT_H\n\n",header);
  64.    fputs("#ifdef __cplusplus\n",header);
  65.    fputs("extern \"C\" {\n",header);
  66.    fputs("#endif\n\n",header);
  67.    fputs("int ui_loadtext(char *fname,char *vers);\n",header);
  68.    fputs("void ui_unloadtext(void);\n",header);
  69.    fputs("char *ui_filetext(unsigned pos);\n",header);
  70.    fputs("char **ui_filearray(unsigned pos);\n",header);
  71.    fputs("extern char **ui_text;\n\n",header);
  72.  
  73.    /* start of .ETF (filename+version), checksum and counters */
  74.    fprintf(etf,"%s%s\032",fname,argc==3?argv[2]:"");
  75.    headlen=strlen(fname)+strlen(argc==3?argv[2]:"")+1;
  76.    filpos1=ftell(etf);
  77.    fwrite(&checksum,sizeof(checksum),1,etf);
  78.    fwrite(&memcount,sizeof(memcount),1,etf);
  79.    fwrite(&filcount,sizeof(memcount),1,etf);
  80.    fwrite(&memoffset,sizeof(memoffset),1,etf);
  81.  
  82.    /* count lines, determine offsets and write to .ETF and UI_TEXT.H */
  83.    while (fgets(buf,255,txt)) {
  84.       l=strlen(buf)-1; if (buf[l]=='\n') buf[l]='\0';
  85.       lines++;
  86.       if (!buf[0]) continue;
  87.       if (buf[0]=='#') { /* comment */
  88.          if (!strcmp(buf+1,"FILE")) {
  89.             if (file) goto fatal;
  90.             file=TRUE; filpos2=ftell(etf);
  91.          }
  92.          continue;
  93.       }
  94.       p=strchr(buf,' '); if (!p) goto fatal;
  95.       *p=0; p++; while (*p==' ') p++;
  96.       len=strlen(p); if (!strcmp(p,"-")) len=0;
  97.       l=strlen(buf)-1;
  98.       if (buf[l]=='[') { /* array identifier */
  99.          if (l>0) { /* first element */
  100.             buf[l]='\0'; array=TRUE; multi=FALSE;
  101.             if (file) fprintf(header,"#define %-30s (ui_filearray(%u))\n",buf,filcount);
  102.             else      fprintf(header,"#define %-30s (ui_text+%u)\n",buf,memcount);
  103.          } else { /* next element */
  104.             if (!array) goto fatal;
  105.          }
  106.          if (file) {
  107.             fwrite(&filoffset,sizeof(filoffset),1,etf);
  108.             filoffset+=len+1; filcount++;
  109.             if (l>0) filoffset+=sizeof(arrsize);
  110.          } else {
  111.             fwrite(&memoffset,sizeof(memoffset),1,etf);
  112.             memoffset+=len+1; memcount++;
  113.          }
  114.       } else if (buf[l]=='/') { /* multi line text */
  115.          if (l>0) { /* first line */
  116.             buf[l]='\0'; multi=TRUE; array=FALSE;
  117.             if (file) {
  118.                fprintf(header,"#define %-30s ui_filetext(%u)\n",buf,filcount);
  119.                fwrite(&filoffset,sizeof(filoffset),1,etf);
  120.                filoffset+=len+1; filcount++;
  121.             } else {
  122.                fprintf(header,"#define %-30s ui_text[%u]\n",buf,memcount);
  123.                fwrite(&memoffset,sizeof(memoffset),1,etf);
  124.                memoffset+=len+1; memcount++;
  125.             }
  126.          } else { /* next line */
  127.             if (file) filoffset+=len+strlen(lf);
  128.                  else memoffset+=len+strlen(lf);
  129.             if (!multi) goto fatal;
  130.          }
  131.       } else { /* normal identifier */
  132.          array=FALSE; multi=FALSE;
  133.          if (file) {
  134.             fprintf(header,"#define %-30s ui_filetext(%u)\n",buf,filcount);
  135.             fwrite(&filoffset,sizeof(filoffset),1,etf);
  136.             filoffset+=len+1; filcount++;
  137.          } else {
  138.             fprintf(header,"#define %-30s ui_text[%u]\n",buf,memcount);
  139.             fwrite(&memoffset,sizeof(memoffset),1,etf);
  140.             memoffset+=len+1; memcount++;
  141.          }
  142.       }
  143.    }
  144.    if (file) fwrite(&filoffset,sizeof(filoffset),1,etf);
  145.  
  146.    /* encrypt lines and write to .ETF */
  147.    rewind(txt); array=multi=file=FALSE;
  148.    strcpy(linefeed,lf); for (l=0; linefeed[l]; l++) linefeed[l]^=UIT_ENCRYPT;
  149.    while (fgets(buf,255,txt)) {
  150.       l=strlen(buf)-1; if (buf[l]=='\n') buf[l]='\0';
  151.       if (!buf[0]) continue;
  152.       if (buf[0]=='#') { /* comment */
  153.          if (!strcmp(buf+1,"FILE")) file=TRUE;
  154.          continue;
  155.       }
  156.       p=strchr(buf,' ');
  157.       *p=0; p++; while (*p==' ') p++;
  158.       len=strlen(p); if (!strcmp(p,"-")) p[0]='\0';
  159.       for (q=p; *q=='_'; q++) *q=' ';
  160.       for (q=p+len-1; *q=='_'; q--) *q=' ';
  161.       l=strlen(buf)-1; len=strlen(p)+1;
  162.       if (buf[l]=='[') { /* array identifier */
  163.          if (file) {
  164.             if (l>0) {
  165.                filpos3=ftell(etf);
  166.                fwrite(&arrsize,sizeof(arrsize),1,etf);
  167.                array=TRUE; arrsize=1;
  168.             } else arrsize++;
  169.          }
  170.          if (multi) putc('\0'^UIT_ENCRYPT,etf);
  171.          multi=FALSE;
  172.       } else if (buf[l]=='/') { /* multi line text */
  173.          if (l==0 && multi) { fputs(linefeed,etf); checksum+=lfchk; }
  174.          if (l>0) {
  175.             if (file && array) {
  176.                checksum+=writesize(etf,filpos3,arrsize);
  177.                array=FALSE;
  178.             }
  179.             if (multi) putc('\0'^UIT_ENCRYPT,etf);
  180.             multi=TRUE;
  181.          }
  182.       } else { /* normal identifier */
  183.          if (file && array) {
  184.             checksum+=writesize(etf,filpos3,arrsize);
  185.             array=FALSE;
  186.          }
  187.          if (multi) putc('\0'^UIT_ENCRYPT,etf);
  188.          multi=FALSE;
  189.       }
  190.       for (q=p; *q; q++) {
  191.          checksum+=(unsigned char)(*q); *q^=UIT_ENCRYPT; putc(*q,etf);
  192.       }
  193.       if (!multi) putc('\0'^UIT_ENCRYPT,etf);
  194.    }
  195.    if (multi) putc('\0'^UIT_ENCRYPT,etf);
  196.    if (file && array) checksum+=writesize(etf,filpos3,arrsize);
  197.  
  198.    /* write checksum and number of items and bytes to .ETF */
  199.    fseek(etf,filpos1,SEEK_SET);
  200.    fwrite(&checksum,sizeof(checksum),1,etf);
  201.    fwrite(&memcount,sizeof(memcount),1,etf);
  202.    fwrite(&filcount,sizeof(memcount),1,etf);
  203.    fwrite(&memoffset,sizeof(memoffset),1,etf);
  204.  
  205.    /* correct file item offsets */
  206.    if (filcount) {
  207.       fseek(etf,filpos2,SEEK_SET);
  208.       offset=headlen+sizeof(checksum)+sizeof(memcount)+sizeof(filcount)+
  209.              sizeof(memoffset)+memcount*sizeof(memoffset)+
  210.              (filcount+1)*sizeof(filoffset)+memoffset;
  211.       for (l=0; l<filcount+1; l++) {
  212.          filpos2=ftell(etf);
  213.          fread(&filoffset,sizeof(filoffset),1,etf);
  214.          fseek(etf,filpos2,SEEK_SET); filoffset+=offset;
  215.          fwrite(&filoffset,sizeof(filoffset),1,etf);
  216.          fseek(etf,0,SEEK_CUR); /* to enable read after write ! */
  217.       }
  218.    }
  219.  
  220.    /* end of UI_TEXT.H